/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdRankingSys.Handlers;

import com.SySammy.SdCurrency.sdCCommandExport;
import com.SySammy.sdRankingSy.PersistenceClasses.Players;
import com.SySammy.sdRankingSy.PersistenceClasses.Ranks;
import com.SySammy.sdRankingSys.prop.sdRankProperties;
import com.SySammy.sdRankingSys.sdRS;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class VocationHandler {

    sdRS plugin;
    sdCCommandExport currency = new sdCCommandExport();
    yamlHandler YH = new yamlHandler(plugin);

    public VocationHandler(sdRS plugin) {
        this.plugin = plugin;
    }

    public ArrayList<String> VocationList() {
        ArrayList<String> Vocations = new ArrayList<String>();
        for (int x = 0; x < YH.NumberOfVocations(); x++) {
            Vocations.add(YH.getVocationNames(x));
        }
        return Vocations;
    }

    public boolean isNewPlayer(Player ply) {
        Player p = ply;
        String worldName = p.getWorld().getName();
        File playerDat = new File(worldName + "/players/" + p.getName() + ".dat");
        if (!playerDat.exists()) {
            return true;
        }
        return false;
    }

    public boolean HasVocation(Player ply) {
        String p = ply.getName();
        Players persistenceClass = plugin.getDatabase().find(Players.class).where().ieq("PlayerName", p).findUnique();
        if (persistenceClass == null || persistenceClass.getRanks().getVocation().equals(sdRankProperties.InitialVocation)) {
            return false;
        }
        return true;
    }

    public String PlyVocation(Player ply) {
        String p = ply.getName();
        Players persistenceClass = plugin.getDatabase().find(Players.class).where().ieq("PlayerName", p).findUnique();
        return persistenceClass.getRanks().getVocation();
    }

    public String PlyVocationLvl(Player ply) {
        String p = ply.getName();
        Players persistenceClass = plugin.getDatabase().find(Players.class).where().ieq("PlayerName", p).findUnique();
        return persistenceClass.getRanks().getVocationLvl();
    }

    public int PlyExpRate(Player ply) {
        String p = ply.getName();
        Players persistenceClass = plugin.getDatabase().find(Players.class).where().ieq("PlayerName", p).findUnique();
        return persistenceClass.getRanks().getExp();
    }

    private Players OpenDataBase(Player ply) {
        String p = ply.getName();
        Players persistenceClass = plugin.getDatabase().find(Players.class).where().ieq("PlayerName", p).findUnique();
        if (persistenceClass == null) {
            persistenceClass = new Players();
        }
        return persistenceClass;
    }

    public void RegNewPlayer(Player p) {
        Players plyReg = OpenDataBase(p);
        plyReg.setPlayerName(p.getName());
        Ranks newRank = new Ranks();
        newRank.setVocation(YH.getVocationNames(0));
        newRank.setVocationLvl("newcomer");
        newRank.setVocationIndex(-1);
        plyReg.setRanks(newRank);
        plugin.getDatabase().save(plyReg);
        List<String> permissions = YH.getPermissions(YH.getVocationNames(0), "newcomer");
        for (int x = 0; x < permissions.size(); x++) {
            plugin.Permissions.addUserPermission(p.getWorld().getName(), p.getName(), (String) permissions.get(x));
        }
        plugin.Permissions.saveAll();
        plugin.Permissions.reload();
    }

    private void NewVocation(Player p, String vocation) {
        List<String> levels = YH.getLevels(vocation);
        Players plyReg = OpenDataBase(p);
        plyReg.getRanks().setVocation(vocation);
        plyReg.getRanks().setVocationLvl((String) levels.toArray()[0]);
        plyReg.getRanks().setVocationIndex(0);
        plyReg.getRanks().setExp(0);
        plugin.getDatabase().save(plyReg);
    }

    public void RegVocation(Player p, String vocation) {
        if (!YH.VocationtExists(vocation)) {
            p.sendMessage(ChatColor.RED + "That vocation does not exist.");
            return;
        }

        if (GetExp(p) < YH.getExp(vocation, "apprentice")) {
            p.sendMessage(ChatColor.RED + "You don't have enough experience to join that Vocation");
            return;
        }

        if (!HasVocation(p)) {
            NewVocation(p, vocation);
            p.sendMessage(ChatColor.GREEN + "Congrats, you have become a great : " + ChatColor.GOLD + vocation + " apprentice.");
            return;
        }

        if (!currency.HasMoney(p.getName(), sdRankProperties.CostOfNewVocation)) {
            p.sendMessage(ChatColor.RED + "You don't have enough money to change vocation: " + ChatColor.GOLD + sdRankProperties.CostOfNewVocation + " " + currency.GetCurrency());
            return;

        } else {
            currency.TakeMoney(p.getName(), sdRankProperties.CostOfNewVocation);
            NewVocation(p, vocation);
            p.sendMessage(ChatColor.GREEN + "Congrats, you have become a great : " + ChatColor.GOLD + vocation + " apprentice.");
            return;
        }
    }

    public int GetExp(Player p) {
        Players plyReg = OpenDataBase(p);
        return plyReg.getRanks().getExp();
    }

    public void AddExp(Player p, int exp) {
        Players plyReg = OpenDataBase(p);
        plyReg.getRanks().setExp(PlyExpRate(p) + exp);
        String level = plyReg.getRanks().getVocationLvl();
        String Vocation = plyReg.getRanks().getVocation();
        int CurrentExp = PlyExpRate(p) + exp;
        plugin.getDatabase().save(plyReg);
        p.sendMessage(ChatColor.GREEN + "Exp earned: " + exp);
        WillItLevel(p, CurrentExp, Vocation, level, plyReg);
    }

    public int LvlUpExp(Player p) {
        Players plyReg = OpenDataBase(p);
        int oldLevelIndex = plyReg.getRanks().getVocationIndex();
        boolean endOfVocation = plyReg.getRanks().isVocationEnd();
        if (plyReg.getRanks().getVocationIndex() == -1) {
            return YH.getExp(PlyVocation(p), PlyVocationLvl(p));
        }
        String newLevel = getNewLevel(p, PlyVocation(p), endOfVocation, oldLevelIndex, plyReg, PlyVocationLvl(p));
        return YH.getExp(PlyVocation(p), newLevel);
    }

    private void WillItLevel(Player p, int CurrentExp, String Vocation, String level, Players plyReg) {
        int oldLevelIndex = plyReg.getRanks().getVocationIndex();
        boolean endOfVocation = plyReg.getRanks().isVocationEnd();
        String newLevel = getNewLevel(p, Vocation, endOfVocation, oldLevelIndex, plyReg, level);

        if (plyReg.getRanks().isVocationEnd() == true) {
            return;
        }

        if (CurrentExp >= YH.getExp(Vocation, newLevel) && plyReg.getRanks().getVocationIndex() != -1) {
            LevelUp(p, CurrentExp);

        } else {
            return;
        }
    }

    private String getNewLevel(Player p, String vocation, boolean endOfVocation, int oldLevelIndex, Players plyReg, String oldLevel) {
        List<String> levels = YH.getLevels(vocation);
        if (oldLevelIndex + 1 >= levels.toArray().length && endOfVocation == false) {
            p.sendMessage(ChatColor.GREEN + "Congrats, you reach the top of your Vocation");
            plyReg.getRanks().setVocationEnd(true);
            plugin.getDatabase().save(plyReg);
            return "END_OF_VOCATION";
        }
        if (plyReg.getRanks().isVocationEnd() == true) {
            return oldLevel;
        }
        return (String) levels.toArray()[oldLevelIndex + 1];
    }

    private void LevelUp(Player p, int exp) {
        Players plyReg = OpenDataBase(p);
        String Vocation = plyReg.getRanks().getVocation();
        String oldLevel = plyReg.getRanks().getVocationLvl();
        int oldLevelIndex = plyReg.getRanks().getVocationIndex();
        boolean endOfVocation = plyReg.getRanks().isVocationEnd();
        String newLevel = getNewLevel(p, Vocation, endOfVocation, oldLevelIndex, plyReg, oldLevel);
        if (newLevel.equals("END_OF_VOCATION")) {
            return;
        } else {
            plyReg.getRanks().setVocationLvl(newLevel);
            plyReg.getRanks().setVocationIndex(oldLevelIndex + 1);
            plyReg.getRanks().setNumbPerks(YH.getPerkQnt(Vocation, newLevel));
            plugin.getDatabase().save(plyReg);
            p.sendMessage(ChatColor.GREEN + "Congrats, you have become a great : " + ChatColor.GOLD + Vocation + " " + newLevel);
            WillItLevel(p, exp, Vocation, newLevel, plyReg);
        }
    }
}
